/*
 * @Date: 2021-09-10 15:40:11
 * @Author: Acckno1
 * @LastEditTime: 2021-09-10 16:32:45
 * @Description: 
 */
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 1e4 + 10;

struct Record {
    string idc;
    string level;
    int score;
    string site, date;

    bool operator< (const Record & r1) const {
        if (level != r1.level) return level > r1.level;
        if (score != r1.score) return score > r1.score;
        return idc < r1.idc;
    }
}r[N];

int n, m;
unordered_map<string, unordered_map<string, int> > dt; // k:date v[k:site, v:cnt]
unordered_map<string, vector<int>> st; // k:site, v:idx


bool cmp(const pair<string, int > & s1, const pair<string, int > s2) {
    if (s1.second != s2.second) return s1.second > s2.second;
    return s1.first < s2.first;
}

void print(int type, string term) {
    bool flag = false;
    if (type == 1) {
        for (int i = 0; i < n; i ++ ) 
            if (r[i].level == term) 
            {
                printf("%s %d\n", r[i].idc.c_str(), r[i].score);
                flag = true;
            }
        if (!flag) printf("NA\n");
    } else if (type == 2) {
        if (st.count(term)) {
            int ans = 0;
            for (auto& id : st[term])
                ans += r[id].score;
            printf("%d %d\n", (int)st[term].size(), ans);
        } else printf("NA\n");
    } else if (type == 3) {
        if (dt.count(term)) {
            vector<pair<string, int >> v; 
            for (auto & d : dt[term])
                v.push_back(mk(d.first, d.second));
            
            sort(v.begin(), v.end(), cmp);
            for (auto& d : v) 
                printf("%s %d\n", d.first.c_str(), d.second);
        } else printf("NA\n");
    } else printf("NA\n");
}

inline void solution() {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i ++ ) {
        char id[15];
        scanf("%s%d", id, &r[i].score);
        r[i].idc = id;
        r[i].level = r[i].idc.substr(0, 1);
        r[i].site = r[i].idc.substr(1, 3);
        r[i].date = r[i].idc.substr(4, 6);
        dt[r[i].date][r[i].site] ++ ;
    }
    sort(r, r + n);
    for (int i = 0; i < n; i ++ ) {
        string site = r[i].site;
        st[site].push_back(i);
    }
    for (int i = 1; i <= m; i ++ ) {
        int type; string term;
        char t[20];
        scanf("%d%s", &type, t);
        term = t;
        printf("Case %d: %d %s\n", i, type, term.c_str());
        print(type, term);
    }

}

int main() {
    // freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}